package com.twoGroup.boot.controller;

import com.alipay.api.AlipayApiException;
import com.twoGroup.boot.pojo.*;
import com.twoGroup.boot.service.CartService;
import com.twoGroup.boot.service.ClientService;
import com.twoGroup.boot.service.OrderService;
import com.twoGroup.boot.service.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


/**
 * @ClassName
 * @Description TODO
 * @Author Xuyujie
 * @Date 2021/5/12 11:36
 * @Version 1.0
 **/
/* 订单接口 */
@Controller
@RequestMapping(value = "Pay")
@Api(tags = "支付接口")
public class PayController {
    public static Logger logger = LoggerFactory.getLogger(PayController.class);
    @Resource
    private PayService payService;//调用支付服务
    @Autowired
    private OrderService orderService;
    @Autowired
    private CartService cartService;
    @Autowired
    private ClientService clientService;
    /*阿里支付*/
    @PostMapping(value = "alipay",produces = {"text/html;charset=UTF-8"})
    @ApiOperation(value = "用于支付")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "subject",value = "商品名称",required = true),
                    @ApiImplicitParam(name="total_amount",value = "订单总价格",required = true),
                    @ApiImplicitParam(name = "body",value = "商品描述",required = true)
            }
    )
    @ResponseBody
    public String alipay(HttpSession session) throws AlipayApiException {
        logger.info(session.getId());
        OrderAndAddress order=(OrderAndAddress) session.getAttribute("orderAndAddress");
        if(order==null){
            order=(OrderAndAddress) session.getAttribute("OrderInPageSession");
        }
        String out_trade_no=order.getOrderNo();
//        GoodsAndShopAndGoodsTypeAndGoodsBrand goodsAndShopAndGoodsTypeAndGoodsBrand = clientService.queryGoodsByGoodsId(order.getGoodsId());
        AlipayBean alipayBean=new AlipayBean();
        int allPrice;
        allPrice=(int) order.getAllPrice();
//        alipayBean.setBody(goodsAndShopAndGoodsTypeAndGoodsBrand.getGoodsBrand()+goodsAndShopAndGoodsTypeAndGoodsBrand.getShopName());
//        alipayBean.setOut_trade_no(out_trade_no);
//        alipayBean.setTotal_amount(new StringBuffer(""+allPrice));
//        alipayBean.setSubject(goodsAndShopAndGoodsTypeAndGoodsBrand.getGoodsName());
        return payService.aliPay(alipayBean);
    }

    @PostMapping(value = "orderPay", produces = {"text/html;charset=UTF-8"})
    @ApiOperation(value = "用于用户未支付订单的支付")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "subject",value = "商品名称",required = true),
                    @ApiImplicitParam(name="total_amount",value = "订单总价格",required = true),
                    @ApiImplicitParam(name = "out_trade_no",value = "订单号",required = true),
                    @ApiImplicitParam(name = "goodsId",value = "商品id",required = true)
            }
    )
    @ResponseBody
    public String orderPay(Integer goodsId,String subject,Integer total_amount,String  out_trade_no,HttpSession session) throws AlipayApiException {
        AlipayBean alipayBean=new AlipayBean();
        alipayBean.setOut_trade_no(out_trade_no);
        alipayBean.setTotal_amount(new StringBuffer(""+total_amount));
        alipayBean.setSubject(subject);
        alipayBean.setBody(subject.substring(6));
        session.setAttribute("out_trade_no",out_trade_no);
        session.setAttribute("goodsId",goodsId);
        return payService.aliPay(alipayBean);
    }

    @GetMapping("/paySuccess")
    public String paySuccess(HttpSession session){
        logger.info(session.getId());
        User user =(User) session.getAttribute("user");
        if(session.getAttribute("out_trade_no")!=null){
            Integer goodsId =(Integer) session.getAttribute("goodsId");
            String out_trade_no = (String)session.getAttribute("out_trade_no");
            int i=orderService.applySuccess(user.getUserId(),goodsId,out_trade_no);
            return "/";
        }
        OrderAndAddress orders=(OrderAndAddress) session.getAttribute("OrderInCartSession");
        if(orders==null){
            orders=(OrderAndAddress) session.getAttribute("OrderInPageSession");
            logger.info(String.valueOf(orders));
            int i=orderService.applySuccess(user.getUserId(),orders.getGoodsId(),orders.getOrderNo());
            return "/";
        }
        int i = orderService.applySuccess(user.getUserId(),orders.getGoodsId(),orders.getOrderNo());
        int i1 = cartService.deleteAllGoodsCart(user.getUserId());
        return "/";
    }
}